home *** CD-ROM | disk | FTP | other *** search
/ PC User 2003 September / Australian PC User - September 2003 (CD1).iso / magstuff / web / files / dwmx61.exe / Disk1 / data1.cab / Configuration_En / Translators / UD4-ColdFusion Live Data.js < prev    next >
Encoding:
JavaScript  |  2002-11-25  |  9.9 KB  |  291 lines

  1. // Copyright 2001 Macromedia, Inc. All rights reserved.
  2.  
  3. var liveDebugTranslator   = false;
  4. var liveMacBeforeFileName = "Desktop:LiveBeforeTranslation.txt";
  5. var liveMacAfterFileName  = "Desktop:LiveAfterTranslation.txt";
  6. var liveWinBeforeFileName = "C:\\LiveBeforeTranslation.txt";
  7. var liveWinAfterFileName  = "C:\\LiveAfterTranslation.txt";
  8.  
  9. var beginLock     = "<mm:beginlock translatorClass=\"MM_LIVE_DATA\" type=\"MM_CFML\" orig=\"%s\">";
  10. var endLock       = "<mm:endlock>";
  11. var translatedAttr    = "mmvisible=false";
  12.  
  13. var serverGeneratedInclude = "\n<SCRIPT LANGUAGE=\"JavaScript\" TYPE=\"text/javascript\" SRC=\"/CFIDE/scripts/cfform.js\"></SCR"+"IPT>\n";
  14. var serverGeneratedScript = "function  _CF_";
  15. var serverGeneratedObject = "CLASSID=\"clsid:D702FBF4-EE60-11d0-BD5B-00A0C91F4635\"";
  16. var serverGeneratedMetaTag = "<META content=\"MSHTML 6.00.2600.0\" name=GENERATOR>"
  17.  
  18.  
  19. function removeHeadOpenTagBegin(name, offset)
  20. {
  21.   if (name.toUpperCase() == "HEAD")
  22.     headStart = offset;
  23. }
  24.  
  25.  
  26.  
  27. function removeHeadCloseTagBegin(name, offset)
  28. {
  29.   closeTagIsHead = (name.toUpperCase() == "HEAD") ? true : false;
  30.  
  31.   if (closeTagIsHead)
  32.     numHeadsFound++;
  33. }
  34.  
  35.  
  36.  
  37. function removeHeadCloseTagEnd(offset)
  38. {
  39.   if (closeTagIsHead)
  40.     headEnd = offset;
  41. }
  42.  
  43.  
  44.  
  45. function removeSecondHead(inStr)
  46. {
  47.     // Create an object to hold pointers to the callback funcs
  48.     var callbackObj = new Object();
  49.     callbackObj.openTagBegin  = removeHeadOpenTagBegin;
  50.     callbackObj.closeTagBegin = removeHeadCloseTagBegin;
  51.     callbackObj.closeTagEnd   = removeHeadCloseTagEnd;
  52.  
  53.     // Scan the inStr looking for a second head tag
  54.     numHeadsFound = 0;
  55.   headStart = -1;
  56.   headEnd = -1;
  57.   dreamweaver.scanSourceString(inStr, callbackObj);
  58.  
  59.     // If second head found, return string without it
  60.   if (numHeadsFound > 1)
  61.     return inStr.substring(0, headStart) + 
  62.            inStr.substring(headEnd, inStr.length);
  63.  
  64.     // No second head found, so just return inStr
  65.     return inStr;
  66. }
  67.  
  68.  
  69.  
  70. function reportError(docNameStr, inStr)
  71. {
  72.     var translated, errorStr, extraHead, index;
  73.  
  74.   // We're going to abort, so first clean up 
  75.   LiveDataTranslator.cleanup();
  76.  
  77.   // The error message will have the wrong column numbers because we've
  78.   // inserted our proprietary labels.  Send file to server again without
  79.   // the labels.
  80.   translated = dreamweaver.liveDataTranslate(docNameStr, inStr);
  81.   errorStr = translated.data;
  82.  
  83.   // Our parser gets confused if it finds a second head in the
  84.   // HTML string.  Remove the head that's added by ColdFusion.
  85.   errorStr = removeSecondHead(errorStr);
  86.  
  87.   // Tell Dreamweaver to display the error message
  88.   dreamweaver.setLiveDataError(errorStr);
  89. }
  90.  
  91.  
  92.  
  93. // If a tag of the specified type surrounds the character at str[index],
  94. // then remove the entire contents of the tag.
  95. function removeTag(tagOpen, tagClose, str, index)
  96. {
  97.     var startIndex = str.lastIndexOf(tagOpen, index);
  98.     var endIndex = str.indexOf(tagClose, index);
  99.     var length = str.length;
  100.     var outStr;
  101.  
  102.     // If opening or closing tag not found, then bail
  103.     if (startIndex == -1 || endIndex == -1)
  104.         return str;
  105.  
  106.     // Add text before tag's opening to outStr
  107.     if (startIndex > 0)
  108.         outStr = str.substring(0, startIndex);
  109.     else
  110.         outStr = "";
  111.  
  112.     // Add text after tag's closing to outStr
  113.     endIndex = endIndex + tagClose.length;
  114.     if (endIndex < length - 1)
  115.         outStr = outStr + str.substring(endIndex, length);
  116.  
  117.     return outStr;
  118. }
  119.  
  120.  
  121.  
  122. function liveDataTranslateMarkup(docNameStr, siteRootStr, inStr)
  123. {
  124.   var labels, translated, outStr, initTags, labelsAndMarker;
  125.  
  126.   if (liveDebugTranslator)
  127.   {
  128.     if ( "macos" == DWfile.getPlatform() )
  129.       DWfile.write( liveMacBeforeFileName, inStr );
  130.     else
  131.       DWfile.write( liveWinBeforeFileName, inStr );
  132.   }
  133.  
  134.   // Refuse to translate if the document's server model is not Cold Fusion
  135.   if (dw.getDocumentDOM().serverModel.getFolderName() != "UD4-ColdFusion")
  136.     return "";
  137.  
  138.   // Refuse to translate if we're not inside a site
  139.   if (siteRootStr.length == 0)
  140.     return "";
  141.  
  142.   // Don't try to translate an empty document
  143.   if (inStr.length == 0)
  144.     return "";
  145.  
  146.   // Send init tags to the server and request requst them
  147.   initTags = dreamweaver.getLiveDataInitTags();
  148.   if (initTags.length > 0)
  149.   {
  150.     // Add a marker at the end so we can check for successful completion
  151.     initTags = initTags + "<!-- MMDW:success -->";
  152.  
  153.     // Send file to server and get the response
  154.     translated = dreamweaver.liveDataTranslate(docNameStr, initTags);
  155.  
  156.     // If we can't find our marker, report that an error occurred
  157.     if (translated.data.lastIndexOf("<!-- MMDW:success -->") == -1)
  158.     {
  159.       if (translated.data.length > 0)
  160.         reportError(docNameStr, initTags);
  161.       return "";
  162.     }
  163.   }
  164.  
  165.   // Specify which tags are cfml: everything with a "cf" prefix
  166.   prefixArray = new Array(1);
  167.   prefixArray[0] = "cf";
  168.  
  169.   // Specify which tags will NOT contain editable HTML in the returned doc
  170.   nonHtmlArray = new Array(10);
  171.   nonHtmlArray[0] = "cfgrid";
  172.   nonHtmlArray[1] = "cfhttp";
  173.   nonHtmlArray[2] = "cfmail";
  174.   nonHtmlArray[3] = "cfquery";
  175.   nonHtmlArray[4] = "cfreport";
  176.   nonHtmlArray[5] = "cfscript";
  177.   nonHtmlArray[6] = "cftree";
  178.   nonHtmlArray[7] = "cfrow";
  179.   nonHtmlArray[8] = "cftable";
  180.   nonHtmlArray[9] = "cfstoredproc";
  181.  
  182.   // Wrap labels around the cfml tags
  183.   LiveDataTranslator.initialize();
  184.   LiveDataTranslator.beginPreTranslate("<!---", "--->", prefixArray, nonHtmlArray);
  185.   dreamweaver.scanSourceString(inStr, LiveDataTranslator);
  186.   labels = LiveDataTranslator.endPreTranslate(inStr);
  187.  
  188.     // The ColdFusion server will replace all the escaped "##" characters that
  189.     // are inside cfoutput tags with an unescaped "#".  When we do the "diff"
  190.     // below, we want hashes inside cfoutput tags to be escaped again.  The
  191.     // solution is to make every "##" be a "####" right now, so that those
  192.     // previously-escaped "##" characters come back from the server as "##".
  193.     labels = LiveDataTranslator.replaceTextInsideTag(labels,
  194.         "cfoutput", "##", "####");
  195.  
  196.   // Add a marker at the end so we can check for successful completion
  197.   labelsAndMarker = labels + "<!-- MMDW:success -->";
  198.  
  199.     // If we send a file with Mac-style "\r" line endings to a Unix server,
  200.     // the server will ignore the "\r" characters (line ending is "\n" on 
  201.     // Unix) and send back a file with no line endings.  Instead, we'll 
  202.     // convert line endings to "\r\n" before sending to the server.
  203.     labelsAndMarker = LiveDataTranslator.setLineEndingsToMatch("\r\n",
  204.     labelsAndMarker);
  205.  
  206.   // Send file to server and get the response
  207.   translated = dreamweaver.liveDataTranslate(docNameStr, labelsAndMarker);
  208.  
  209.   // If we can't find our marker, report that an error occurred
  210.   index = translated.data.lastIndexOf("<!-- MMDW:success -->");
  211.   if (index == -1)
  212.   {
  213.       if (translated.data.length > 0)
  214.       reportError(docNameStr, inStr);
  215.     LiveDataTranslator.cleanup();
  216.     return "";
  217.   }
  218.   translated.data = translated.data.substr(0, index);
  219.  
  220.   // If the server added script or object tags at the top of the document,
  221.   // then remove them.  Otherwise, they may be added to the document without
  222.   // being inside a locked region.
  223.   var bSomethingFound = false;
  224.   var beforeRemoveTag;
  225.   do
  226.   {
  227.     bSomethingFound = false;
  228.     if (index != -1 && inStr.indexOf(serverGeneratedInclude) == -1)
  229.     {
  230.       beforeRemoveTag = translated.data;
  231.       translated.data = translated.data.substring(0, index) + translated.data.substring(index + serverGeneratedInclude.length);
  232.       if (beforeRemoveTag != translated.data)
  233.         bSomethingFound = true;
  234.     }
  235.     index = translated.data.indexOf(serverGeneratedScript);
  236.     if (index != -1 && inStr.indexOf(serverGeneratedScript) == -1)
  237.     {
  238.       beforeRemoveTag = translated.data;
  239.       translated.data = removeTag("\n\r<script", "</scr" + "ipt>\n\r", translated.data, index);
  240.       if (beforeRemoveTag == translated.data)
  241.         translated.data = removeTag("\r\n<script", "</scr" + "ipt>\r\n", translated.data, index);
  242.       if (beforeRemoveTag == translated.data)
  243.         translated.data = removeTag("\n\n<SCRIPT", "</SCR" + "IPT>\n", translated.data, index);
  244.       if (beforeRemoveTag != translated.data)
  245.         bSomethingFound = true;
  246.     }
  247.     index = translated.data.indexOf(serverGeneratedObject);
  248.     if (index != -1 && inStr.indexOf(serverGeneratedObject) == -1)
  249.     {
  250.       beforeRemoveTag = translated.data;
  251.       translated.data = removeTag("\r\n\r\n<OBJECT", "</OBJECT>\r\n\r\n\n\r", translated.data, index);
  252.       if (beforeRemoveTag != translated.data)
  253.         bSomethingFound = true;
  254.     }
  255.     index = translated.data.indexOf(serverGeneratedMetaTag);
  256.     if (index != -1 && inStr.indexOf(serverGeneratedMetaTag) == -1)
  257.     {
  258.       beforeRemoveTag = translated.data;
  259.       translated.data = translated.data.substring(0, index) + translated.data.substring(index + serverGeneratedMetaTag.length);
  260.       if (beforeRemoveTag != translated.data)
  261.         bSomethingFound = true;
  262.     }
  263.   } while (bSomethingFound);
  264.  
  265.   // Some HTTP servers change the line endings in the file
  266.   translated.data = LiveDataTranslator.setLineEndingsToMatch(inStr,
  267.     translated.data);
  268.  
  269.   // If the scripts on the page were not executed or there are no scripts
  270.   // on the page, then just abort
  271.   if (translated.data == labels)
  272.     return "";
  273.  
  274.   // Remove the labels and add lock tags
  275.   LiveDataTranslator.beginPostTranslate();
  276.   dreamweaver.scanSourceString(translated.data, LiveDataTranslator);
  277.   outStr = LiveDataTranslator.endPostTranslate(inStr, translated.data,
  278.     beginLock, endLock, translatedAttr);
  279.   LiveDataTranslator.cleanup();
  280.  
  281.   if (liveDebugTranslator)
  282.   {
  283.     if ( "macos" == DWfile.getPlatform() )
  284.       DWfile.write( liveMacAfterFileName, outStr );
  285.     else
  286.       DWfile.write( liveWinAfterFileName, outStr );
  287.   }
  288.  
  289.   return outStr;
  290. }
  291.